#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def optimize_customer_detail():
    file_path = 'frontend/src/views/crm/components/CustomerDetailNew.vue'

    # 读取文件
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # 新的分组结构
    new_content = '''<template>
  <GlobalDrawer
    v-model="visible"
    title="客户详情"
    size="85%"
    :show-footer-buttons="false"
  >
    <div v-loading="loading" class="customer-detail">
      <!-- 头部摘要信息 -->
      <el-card class="header-card" shadow="never">
        <div class="customer-header">
          <div class="header-left">
            <h2>{{ detail.name }}</h2>
            <div class="header-tags">
              <el-tag :type="getCustomerTypeColor(detail.customer_type)">
                {{ getCustomerTypeText(detail.customer_type) }}
              </el-tag>
              <el-tag v-if="detail.is_cooperation" type="success">已合作</el-tag>
              <el-tag v-else type="info">未合作</el-tag>
              <el-tag :type="getLevelTagType(detail.level)">
                {{ getLevelText(detail.level) }}
              </el-tag>
              <el-tag v-if="detail.member_status === 1" type="warning">会员客户</el-tag>
            </div>
          </div>
          <div class="header-actions">
            <el-button @click="handleFollowup">快速跟进</el-button>
            <el-button type="primary" @click="handleEdit">编辑客户</el-button>
          </div>
        </div>
      </el-card>

      <!-- 主体内容区域 -->
      <div class="detail-content">
        <!-- 左侧主信息 -->
        <div class="main-content">
          <!-- 1. 基础信息卡片 -->
          <el-card header="基础信息" class="section">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="客户名称">{{ detail.name || '-' }}</el-descriptions-item>
              <el-descriptions-item label="联系电话">{{ detail.phone || '-' }}</el-descriptions-item>
              <el-descriptions-item label="负责人">{{ detail.user_name || '-' }}</el-descriptions-item>
              <el-descriptions-item label="所属部门">{{ detail.dept_name || '-' }}</el-descriptions-item>
              <el-descriptions-item label="客户来源">{{ getSourceText(detail.source) || '-' }}</el-descriptions-item>
              <el-descriptions-item label="客户等级">{{ getLevelText(detail.level) || '-' }}</el-descriptions-item>
              <el-descriptions-item label="重要程度">{{ getImportantLevelText(detail.important_level) || '-' }}</el-descriptions-item>
              <el-descriptions-item label="客户状态">
                <el-tag :type="getStatusTagType(detail.status)">
                  {{ getStatusText(detail.status) }}
                </el-tag>
              </el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 2. 企业信息卡片（仅企业客户显示） -->
          <el-card v-if="detail.customer_type === 2" header="企业信息" class="section">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="行业">{{ getIndustryText(detail.industry) || '-' }}</el-descriptions-item>
              <el-descriptions-item label="企业规模">{{ getScaleText(detail.scale) || '-' }}</el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 3. 证件信息卡片 -->
          <el-card header="证件信息" class="section">
            <el-descriptions :column="2" border>
              <!-- 企业客户显示法定代表人 -->
              <el-descriptions-item v-if="detail.customer_type === 2" label="法定代表人">
                {{ detail.legal_person || '-' }}
              </el-descriptions-item>
              <el-descriptions-item label="证件类型">{{ getIdTypeText(detail.id_type) || '-' }}</el-descriptions-item>
              <el-descriptions-item label="证件号码">{{ detail.id_number || '-' }}</el-descriptions-item>
              <el-descriptions-item label="客户属性">{{ getCustomerAttributeText(detail.customer_attribute) || '-' }}</el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 4. 联系人信息卡片 -->
          <el-card header="联系人信息" class="section">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="联系人">{{ detail.contact_name || '-' }}</el-descriptions-item>
              <el-descriptions-item label="联系人电话">{{ detail.contact_phone || '-' }}</el-descriptions-item>
              <el-descriptions-item label="邮箱">{{ detail.email || '-' }}</el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 5. 营销信息卡片 -->
          <el-card header="营销信息" class="section">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="合作状态">
                <el-tag v-if="detail.is_cooperation" type="success">已合作</el-tag>
                <el-tag v-else type="info">未合作</el-tag>
              </el-descriptions-item>
              <el-descriptions-item v-if="detail.is_cooperation" label="合作时间">
                {{ formatTime(detail.cooperation_time) }}
              </el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 6. 地址信息卡片 -->
          <el-card header="地址信息" class="section">
            <el-descriptions :column="1" border>
              <el-descriptions-item label="完整地址">
                {{ formatAddress() }}
              </el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 联系人信息表格卡片 -->
          <el-card header="联系人信息" class="section">
            <template #header>
              <div style="display: flex; justify-content: space-between; align-items: center;">
                <span>联系人信息</span>
                <el-button type="primary" size="small" @click="handleAddContact">新增联系人</el-button>
              </div>
            </template>
            <el-table :data="detail.contacts || []" size="small" v-if="detail.contacts && detail.contacts.length > 0">
              <el-table-column prop="name" label="姓名" width="100" />
              <el-table-column prop="position" label="职务" width="120" />
              <el-table-column prop="phone" label="电话" width="120" />
              <el-table-column prop="wechat" label="微信" width="120" />
              <el-table-column prop="email" label="邮箱" width="150" />
              <el-table-column prop="is_primary" label="主要联系人" width="100">
                <template #default="{ row }">
                  <el-tag v-if="row.is_primary" type="success">是</el-tag>
                  <span v-else>-</span>
                </template>
              </el-table-column>
              <el-table-column prop="create_time" label="创建时间" width="140" />
              <el-table-column label="操作" width="150">
                <template #default="{ row }">
                  <el-button type="text" size="small" @click="handleEditContact(row)">编辑</el-button>
                  <el-button type="text" size="small" style="color: #f56c6c;" @click="handleDeleteContact(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-empty v-else description="暂无联系人信息" :image-size="60" />
          </el-card>

          <!-- 跟进记录卡片 -->
          <el-card header="跟进记录" class="section">
            <template #header>
              <div style="display: flex; justify-content: space-between; align-items: center;">
                <span>跟进记录</span>
                <el-button type="primary" size="small" @click="handleAddFollowup">新增跟进</el-button>
              </div>
            </template>
            <el-timeline v-if="detail.followups && detail.followups.length > 0">
              <el-timeline-item
                v-for="item in detail.followups"
                :key="item.id"
                :timestamp="formatTime(item.followup_time)"
                placement="top"
              >
                <el-card>
                  <div class="followup-content">
                    <div class="followup-header">
                      <el-tag size="small">{{ getFollowupTypeText(item.followup_type) }}</el-tag>
                      <el-tag size="small" :type="getFollowupResultTagType(item.followup_result)">
                        {{ getFollowupResultText(item.followup_result) }}
                      </el-tag>
                    </div>
                    <p class="followup-desc">{{ item.content }}</p>
                  </div>
                </el-card>
              </el-timeline-item>
            </el-timeline>
            <el-empty v-else description="暂无跟进记录" :image-size="60" />
          </el-card>

          <!-- 订单信息卡片 -->
          <el-card header="订单信息" class="section">
            <template #header>
              <div style="display: flex; justify-content: space-between; align-items: center;">
                <span>订单信息</span>
                <el-button type="primary" size="small" @click="handleAddOrder">新增订单</el-button>
              </div>
            </template>
            <el-table :data="detail.orders || []" size="small" v-if="detail.orders && detail.orders.length > 0">
              <el-table-column prop="order_no" label="订单编号" width="150" />
              <el-table-column prop="order_type" label="订单类型" width="120">
                <template #default="{ row }">
                  {{ getOrderTypeText(row.order_type) }}
                </template>
              </el-table-column>
              <el-table-column prop="total_amount" label="订单金额" width="120">
                <template #default="{ row }">
                  ¥{{ row.total_amount }}
                </template>
              </el-table-column>
              <el-table-column prop="payment_status" label="付款状态" width="100">
                <template #default="{ row }">
                  <el-tag :type="getPaymentStatusTagType(row.payment_status)">
                    {{ getPaymentStatusText(row.payment_status) }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="create_time" label="创建时间" width="140" />
              <el-table-column label="操作" width="150">
                <template #default="{ row }">
                  <el-button type="text" size="small" @click="handleViewOrder(row)">查看</el-button>
                  <el-button type="text" size="small" @click="handleEditOrder(row)">编辑</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-empty v-else description="暂无订单信息" :image-size="60" />
          </el-card>
        </div>

        <!-- 右侧侧边栏 -->
        <div class="sidebar">
          <!-- 统计信息卡片 -->
          <StatisticsCard :data="detail" />

          <!-- 会员信息卡片（会员客户时显示） -->
          <el-card v-if="detail.member_status === 1" header="会员信息" class="section">
            <el-descriptions :column="1" border size="small">
              <el-descriptions-item label="会员类型">
                {{ getMemberTypeText(detail.member_type) }}
              </el-descriptions-item>
              <el-descriptions-item label="服务期限">
                {{ formatTime(detail.member_start_time) }} 至 {{ formatTime(detail.member_end_time) }}
              </el-descriptions-item>
              <el-descriptions-item label="账号类型">
                {{ detail.is_main_account === 1 ? '主账号' : '子账号' }}
              </el-descriptions-item>
            </el-descriptions>
          </el-card>

          <!-- 标签与备注卡片 -->
          <TagsAndRemarkCard :data="detail" />
        </div>
      </div>
    </div>
  </GlobalDrawer>
</template>'''

    # 保留原有的script部分，只需要在基础信息卡片后添加新卡片
    # 我们使用Python脚本来重新组织内容

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)

    print("✅ 客户详情页面分组优化完成！")
    print("\n优化内容：")
    print("1. 按照表单分组重新组织：")
    print("   - 基础信息卡片")
    print("   - 企业信息卡片（仅企业客户显示）")
    print("   - 证件信息卡片")
    print("   - 联系人信息卡片")
    print("   - 营销信息卡片")
    print("   - 地址信息卡片")
    print("2. 根据客户类型动态显示字段：")
    print("   - 个人客户：不显示法定代表人、行业类型、企业规模")
    print("   - 企业客户：显示所有企业相关字段")
    print("3. 移除了重复的联系人表格")

if __name__ == '__main__':
    optimize_customer_detail()
